<?php

namespace App\Http\Controllers;

use App\Handlers\ImageUploadHandler;
use App\Models\Category;
use App\Models\Comment;
use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth',['except'=>['index','show']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::query()->orderByDesc('updated_at')->with('comments')->with('category')->paginate(20);
        return view('pages.post.index',compact('posts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Category::query()->get();
        return view('pages.post.create',compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,ImageUploadHandler $handler)
    {
        $this->validate($request,[
            'title'=>'required|between:2,30',
            'content'=>'required|between:2,50000',
        ]);
        $data = $request->all(['title','content','image','user_id','category_id']);
        if ($request->image) {
            $image = $handler->save($request->image,'image','post_image');
            $data['image'] = $image['path'] ?:'';
        }
        $post = Post::query()->create($data);
        return redirect()->route('posts.index');


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request,$id)
    {
        $post = Post::query()->find($id);
        $comments = Comment::query()->where('post_id',$id)->orderByDesc('created_at')->with('user')->paginate(10);
        if (!empty($post->slug) && $post->slug != $request->slug) {
            return redirect($post->link(),301);
        }
        return view('pages.post.show',compact('post','comments'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $post = Post::query()->find($id);
        $categories = Category::query()->get();
        return view('pages.post.edit',compact('post','categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @param ImageUploadHandler $handler
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(Request $request, $id,ImageUploadHandler $handler)
    {
        $this->validate($request,[
            'title'=>'required|between:2,30',
            'content'=>'required|between:2,50000',
        ]);
        $data = $request->all(['title','content','user_id','category_id']);
        if ($request->image) {
            $image = $handler->save($request->image,'image','post_image');
            $data['image'] = $image['path'] ?:'';
        }

        $post = Post::query()->find($id);
        $post->update($data);
        return redirect()->route('posts.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Post::query()->find($id)->delete();
        return redirect()->route('posts.index');
    }
}
